package com.hsjk.szwj.module.system.service.service;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.hsjk.szwj.module.system.controller.admin.service.vo.*;
import com.hsjk.szwj.module.system.dal.dataobject.service.TenantServiceDO;
import com.hsjk.szwj.framework.common.pojo.PageResult;

import com.hsjk.szwj.module.system.convert.service.TenantServiceConvert;
import com.hsjk.szwj.module.system.dal.mysql.service.TenantServiceMapper;

import static com.hsjk.szwj.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.hsjk.szwj.module.system.enums.ErrorCodeConstants.*;

/**
 * 套餐服务关联 Service 实现类
 *
 * @author ec50n9
 */
@Service
@Validated
public class TenantServiceServiceImpl implements TenantServiceService {

    @Resource
    private TenantServiceMapper tenantServiceMapper;

    @Override
    public Long createTenantService(TenantServiceCreateReqVO createReqVO) {
        // 插入
        TenantServiceDO tenantService = TenantServiceConvert.INSTANCE.convert(createReqVO);
        tenantServiceMapper.insert(tenantService);
        // 返回
        return tenantService.getId();
    }

    @Override
    public void updateTenantService(TenantServiceUpdateReqVO updateReqVO) {
        // 校验存在
        validateTenantServiceExists(updateReqVO.getId());
        // 更新
        TenantServiceDO updateObj = TenantServiceConvert.INSTANCE.convert(updateReqVO);
        tenantServiceMapper.updateById(updateObj);
    }

    @Override
    public void deleteTenantService(Long id) {
        // 校验存在
        validateTenantServiceExists(id);
        // 删除
        tenantServiceMapper.deleteById(id);
    }

    private void validateTenantServiceExists(Long id) {
        if (tenantServiceMapper.selectById(id) == null) {
            throw exception(TENANT_SERVICE_NOT_EXISTS);
        }
    }

    @Override
    public TenantServiceDO getTenantService(Long id) {
        return tenantServiceMapper.selectById(id);
    }

    @Override
    public List<TenantServiceDO> getTenantServiceList(Collection<Long> ids) {
        return tenantServiceMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<TenantServiceDO> getTenantServicePage(TenantServicePageReqVO pageReqVO) {
        return tenantServiceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<TenantServiceDO> getTenantServiceList(TenantServiceExportReqVO exportReqVO) {
        return tenantServiceMapper.selectList(exportReqVO);
    }

}
